package org.megha.blog.example.part16;

/**
 * A stereo system that can play and record.
 */
public class StereoSystem {

	public static enum State {
		STOPPED,
		PLAYING,
		RECORDING
	}

	private State currentState;

	public StereoSystem() {
		currentState = State.STOPPED;
	}

	/**
	 * @return the current state of the stereo system
	 */
	public State getCurrentState() {
		return currentState;
	}

	/**
	 * Starts playing. Caller must ensure that the stereo system
	 * is not in record before calling play.
	 *
	 * @throws IllegalStateException if the system is recording
	 */
	public void play() {
		if (currentState == State.RECORDING) {
			throw new IllegalStateException("Cannot play while recording");
		}
		currentState = State.PLAYING;
	}

	/**
	 * Starts recording. Caller must ensure that the stereo
	 * system is not in play before calling record.
	 *
	 * @throws IllegalStateException if the system is playing
	 */
	public void record() {
		if (currentState == State.PLAYING) {
			throw new IllegalStateException("Cannot record while playing");
		}
		currentState = State.RECORDING;
	}

	/**
	 * Stops playing or recording.
	 */
	public void stop() {
		currentState = State.STOPPED;
	}
}
